package api

import (
	"github.com/gin-gonic/gin"
	"github.com/nntaoli-project/goex"
	"net/http"
	"quantization_trading/src/models"
	"quantization_trading/src/mycache"
	"quantization_trading/src/server"
	"quantization_trading/src/util"
)

func FindUseAllOrder(c *gin.Context)  {
	var data ReqOrderFund
	c.ShouldBindJSON(&data)
	if x,ok := mycache.OrderCache.Get(data.AccountName);ok{
		c.JSON(http.StatusOK,gin.H{
			"status":  util.SUCCESS,
			"message": x,
		})
	}else {
		c.JSON(http.StatusOK,gin.H{
			"status":  util.ERROR,
			"message": util.GetErrMsg(util.FUND_ERROR),
		})
	}
	//order_list, err := models.FindAllUseOrder(data.AccountName)
	//if err !=nil{
	//	c.JSON(http.StatusOK,gin.H{
	//		"status":  util.ERROR,
	//		"message": util.GetErrMsg(util.FUND_ERROR),
	//	})
	//}else {
	//	c.JSON(http.StatusOK,gin.H{
	//		"status":  util.SUCCESS,
	//		"message": order_list,
	//	})
	//}
}

func FindAllOrder(c *gin.Context)  {
	order_list, err := models.FindAllOrder()
	if err !=nil{
		c.JSON(http.StatusOK,gin.H{
			"status":  util.ERROR,
			"message": util.GetErrMsg(util.FUND_ERROR),
		})
	}else {
		c.JSON(http.StatusOK,gin.H{
			"status":  util.SUCCESS,
			"message": order_list,
		})
	}
}

func FindOrder(c *gin.Context)  {
	var data models.Order
	var os server.OrderServer
	c.ShouldBindJSON(&data)
	a,b :=util.PairChangeCurrency(data.Symbol)
	pair := goex.NewCurrencyPair(goex.NewCurrency(a,""),goex.NewCurrency(b,""))

	if x,ok := mycache.OrderServerCache.Get(data.AccountName);ok{
		os = x.(server.OrderServer)
		order_list,err := os.BinanceSwap.GetFutureOrders([]string{data.OrderId},pair,goex.SWAP_USDT_CONTRACT)
		if err !=nil{
			c.JSON(http.StatusOK,gin.H{
				"status":  util.ERROR,
				"message": util.GetErrMsg(util.FUND_ERROR),
			})
		}else {
			c.JSON(http.StatusOK,gin.H{
				"status":  util.SUCCESS,
				"message": order_list,
			})
		}
	}else {
		c.JSON(http.StatusOK,gin.H{
			"status":  util.NOT_USER_ERROR,
			"message": util.GetErrMsg(util.NOT_USER_ERROR),
		})
	}
}


func LimitOrder(c *gin.Context)  {
	var data models.Order
	var os server.OrderServer
	c.ShouldBindJSON(&data)

	if x,ok := mycache.OrderServerCache.Get(data.AccountName);ok{
		os = x.(server.OrderServer)
		//CheckCancel(data,os)
		order := os.LimitOrder(data.Symbol,goex.FloatToString(data.Price,10),goex.FloatToString(data.OrigQty,10),data.OType)
		if order == nil{
			c.JSON(http.StatusOK,gin.H{
				"status":  util.USE_ORDER_SUSSESS,
				"message": util.GetErrMsg(util.USE_ORDER_SUSSESS),
			})
		}else {
			c.JSON(http.StatusOK,gin.H{
				"status":  util.USE_ORDER_ERROR,
				"message": util.GetErrMsg(util.USE_ORDER_ERROR),
			})
		}

	}else {
		c.JSON(http.StatusOK,gin.H{
			"status":  util.NOT_USER_ERROR,
			"message": util.GetErrMsg(util.NOT_USER_ERROR),
		})
	}

}

func MarketOrder(c *gin.Context)  {
	var data models.Order
	var os server.OrderServer
	c.ShouldBindJSON(&data)

	if x,ok := mycache.OrderServerCache.Get(data.AccountName);ok{
		os = x.(server.OrderServer)
		//CheckCancel(data,os)
		order := os.MarketOrder(data.Symbol,goex.FloatToString(data.OrigQty,10),data.OType)
		if order == nil{
			c.JSON(http.StatusOK,gin.H{
				"status":  util.USE_ORDER_SUSSESS,
				"message": util.GetErrMsg(util.USE_ORDER_SUSSESS),
			})
		}else {
			c.JSON(http.StatusOK,gin.H{
				"status":  util.USE_ORDER_ERROR,
				"message": util.GetErrMsg(util.USE_ORDER_ERROR),
			})
		}
	}else {
		c.JSON(http.StatusOK,gin.H{
			"status":  util.NOT_USER_ERROR,
			"message": util.GetErrMsg(util.NOT_USER_ERROR),
		})
	}
}

//func CheckCancel(od models.Order,os server.OrderServer){
//	var sub_order *models.Order
//	if od.OType == 3 || od.OType == 4{
//		sub_order = models.FindSubOrder(od.ParentId)
//		if sub_order != nil{
//			os.CancelOrder(od.Symbol,sub_order.Cid)
//		}
//	}
//}

func LimitCloseAllOrder(c *gin.Context){
	var data models.Order
	var os server.OrderServer
	c.ShouldBindJSON(&data)

	if x,ok := mycache.OrderServerCache.Get(data.AccountName);ok {
		os = x.(server.OrderServer)
		code,err := os.LimitOrderAllClose(data.Symbol, goex.FloatToString(data.Price, 10))
		if err !=nil{
			c.JSON(http.StatusOK, gin.H{
				"status":  code,
				"message": err.Error(),
			})
		}else {
			c.JSON(http.StatusOK, gin.H{
				"status":  code,
				"message": util.GetErrMsg(code),
			})
		}
	}
}

func MarketCloseAllOrder(c *gin.Context){
	var data models.Order
	var os server.OrderServer
	c.ShouldBindJSON(&data)

	if x,ok := mycache.OrderServerCache.Get(data.AccountName);ok {
		os = x.(server.OrderServer)
		code,err := os.MarketOrderAllClose(data.Symbol)
		if err !=nil{
			c.JSON(http.StatusOK, gin.H{
				"status":  code,
				"message": err.Error(),
			})
		}else {
			c.JSON(http.StatusOK, gin.H{
				"status":  code,
				"message": util.GetErrMsg(code),
			})
		}
	}
}